﻿/*
统一控制urlquery查询参数
*/
function UrlQuery() {
    UrlQuery.prototype.url = new URL(document.location.href);
    UrlQuery.prototype.page = function (domId) {
        this.$dom = null;
        var url = this.url;
        if (domId) {
            this.$dom = $("#" + domId);
            if (this.$dom.length === 0) {
                throw "UrlQuery找不到元素:" + domId;
            }
        } else {
            this.$dom = $(document);
        }
        this.$dom.find(".page-link").each(function (i, e) {
            $(e).on("click", function (e) {
                url.searchParams.set($(this).data("pg-n"), $(this).data("pg-i"));
                location.href = url.href;
            })
        })
    }
    UrlQuery.prototype.events = [];
    /*
     绑定url参数名和值的函数
     */
    UrlQuery.prototype.on = function (name, act) {
        this.events.push({ name: name, act: act });
    }
    /*
     查询/跳转按钮触发
     */
    UrlQuery.prototype.go = function () {
        var events = this.events;
        var url = this.url;
        for (var i = 0; i < events.length; i++) {
            url.searchParams.set(events[i].name, events[i].act());
        }
        location.href = this.url.href;
    }
}
